﻿Imports System.Data
Imports Oracle.DataAccess.Client
Imports Oracle.DataAccess.Types

Public Class EditMed

    Dim objConn As OracleConnection
    Dim dtadapter As OracleDataAdapter
    Dim objCmd As New OracleCommand
    Dim da As OracleDataAdapter
    Dim cb As OracleCommandBuilder
    Dim ds As DataSet
    Dim Udone As New Err
    Dim Done As New Done
    Dim infor As New infor

    Private Sub BT1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BT1.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If ID.Text = "" Then
            infor.ShowDialog()
        Else          
            Med_N.Enabled = True
            C_id.Enabled = True
            Si.Enabled = True
            Prop.Enabled = True
            How.Enabled = True
            Cu.Enabled = True
            M_ID.Enabled = True
            ChangeValue()
            Try
                Dim sql As String = "select * from MEDICINES where MED_ID = '" + ID.Text + "'"
                dtadapter = New OracleDataAdapter(sql, objConn)
                Dim dt As New DataTable
                dtadapter.Fill(dt)

                Med_N.Text = dt.Rows(0)("MED_NAME")
                C_id.Text = dt.Rows(0)("TYPE_ID")
                Si.Text = dt.Rows(0)("AMOUNT_MED")
                Prop.Text = dt.Rows(0)("PROPOTIES")
                How.Text = dt.Rows(0)("HOW_TO_USE")
                Cu.Text = dt.Rows(0)("CAUTION")
                M_ID.Text = dt.Rows(0)("MENUFAC_ID")
            Catch ex As Exception
                MessageBox.Show(ex.Message.ToString())
                Udone.ShowDialog()
                EnableFalse()
                ID.Enabled = True
            End Try
        End If
    End Sub

    Private Sub BT2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BT2.Click
        If ID.Text = "" Or Med_N.Text = "" Or C_id.Text = "" Or Si.Text = "" Or Prop.Text = "" Or C_id.Text = "" Then
            infor.ShowDialog()
        Else
            Editlbl.Visible = True
            editno.Visible = True
            editok.Visible = True
            BT1.Enabled = False
            BT2.Enabled = False
            BT3.Enabled = False
            Button1.Enabled = False
            EnableFalse()
        End If
    End Sub

    Private Sub BT3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BT3.Click
        If ID.Text = "" Or Med_N.Text = "" Or C_id.Text = "" Or Si.Text = "" Or Prop.Text = "" Or C_id.Text = "" Then
            infor.ShowDialog()
        Else
            Dellbl.Visible = True
            Delno.Visible = True
            DelOk.Visible = True
            BT1.Enabled = False
            BT2.Enabled = False
            BT3.Enabled = False
            Button1.Enabled = False
            EnableFalse()
        End If
    End Sub

    Private Sub editok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles editok.Click
        Editlbl.Visible = False
        editno.Visible = False
        editok.Visible = False
        BTTrue()
        Button1.Enabled = True
        ID.Enabled = True

        Try
            Dim sql As String = "Update MEDICINES SET MED_ID = '" + ID.Text + "',MED_NAME = '" + Med_N.Text _
                                + "',TYPE_ID = '" + C_id.Text + "',AMOUNT_MED = '" + Si.Text + "',PROPOTIES = '" + Prop.Text _
                                + "',HOW_TO_USE = '" + How.Text + "',CAUTION = '" + Cu.Text + "',MENUFAC_ID = '" _
                                + M_ID.Text + "' WHERE MED_ID = '" + ID.Text + "'"
            objCmd = New OracleCommand
            With objCmd
                .Connection = objConn
                .CommandText = sql
                .CommandType = CommandType.Text
            End With
            Try
                objCmd.ExecuteNonQuery()
                Done.ShowDialog()
            Catch ex As Exception
                MessageBox.Show(ex.Message.ToString())
                Udone.ShowDialog()
            End Try
        Catch ex As Exception
            Udone.ShowDialog()
        Finally
            objConn.Dispose()
            DataSC()
        End Try
    End Sub

    Private Sub editno_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles editno.Click
        Editlbl.Visible = False
        editno.Visible = False
        editok.Visible = False
        BTTrue()
        Button1.Enabled = True
        ID.Enabled = True
        ChangeValue()
    End Sub

    Private Sub DelOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DelOk.Click
        Dellbl.Visible = False
        Delno.Visible = False
        DelOk.Visible = False
        Button1.Enabled = True
        BTTrue()
        ID.Enabled = True
        Try

            Dim sql As String = "DELETE FROM MEDICINES WHERE MED_ID = '" + ID.Text + "'"
            objCmd = New OracleCommand(sql, objConn)

            Try
                objCmd.ExecuteNonQuery()
                Done.ShowDialog()
            Catch ex As Exception
                Udone.ShowDialog()
            End Try
        Catch ex As Exception
            Udone.ShowDialog()
        Finally
            objConn.Dispose()
            DataSC()
        End Try
    End Sub

    Private Sub Delno_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delno.Click
        Dellbl.Visible = False
        Delno.Visible = False
        DelOk.Visible = False
        Button1.Enabled = True
        BTTrue()
        ID.Enabled = True
        ChangeValue()
    End Sub

    Private Sub EditMed_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dellbl.Visible = False
        Delno.Visible = False
        DelOk.Visible = False
        editno.Visible = False
        editok.Visible = False
        Editlbl.Visible = False
        EnableFalse()
        ID.Enabled = True
        DataSC()
    End Sub

    Private Sub DataSC()
        Dim strConnString As String
        strConnString = Login.strConn
        objConn = New OracleConnection(strConnString)
        objConn.Open()
        Dim cmd2 As New OracleCommand
        Dim sql1 As String = "select * from MEDICINES ORDER BY MED_ID ASC"
        cmd2 = New OracleCommand(sql1, objConn)
        da = New OracleDataAdapter(cmd2)
        cb = New OracleCommandBuilder(da)
        ds = New DataSet()
        da.Fill(ds, "MEDICINES")

        With Me.ID
            .DataSource = ds.Tables("MEDICINES")
            .DisplayMember = "MED_ID"
            .ValueMember = "MED_ID"
        End With
        ChangeValue()
    End Sub

    Private Sub ChangeValue()
        Med_N.Text = ""
        C_id.Text = ""
        Si.Text = ""
        Prop.Text = ""
        How.Text = ""
        Cu.Text = ""
        M_ID.Text = ""
    End Sub

    Private Sub EnableFalse()
        ID.Enabled = False
        Med_N.Enabled = False
        C_id.Enabled = False
        Si.Enabled = False
        Prop.Enabled = False
        How.Enabled = False
        Cu.Enabled = False
        M_ID.Enabled = False
    End Sub

    Private Sub BTTrue()
        BT1.Enabled = True
        BT2.Enabled = True
        BT3.Enabled = True
    End Sub

End Class